﻿#ifndef WEBSERVER_H
#define WEBSERVER_H

//url的最大数量，后续如果太小可以增加
#define WEB_URL_MAXNUM 100

//http响应的头部大小，在构造响应数据时需要根据大小进行
#define WEB_RESPONSE_HEADER_MAX_LENGTH 1024

typedef void (*url_callback)(WebUser *);
#define WEBFUNC(funcName) void funcName(WebUser *web_user)

/**
 * webserver.c
 */
void urlFunc_init();
void urlFuncAdd(char *url,url_callback func,int methods);
void WebServer_init();
int web_packet_in(Client *client, char *readbuf, int *length);
int IsHttpMethod(char *readbuf, int *length);
void webUser_free(WebUser *webUser);
void httpHeader_free(HttpHeader *http_header);
void urlParamList_free(UrlParamList *paramlist);
void web_requestData_free(RequestData *requestData);
void web_responseData_free(ResponseData *responseData);
void responseData_add_header(ResponseData *responseData,char *field,char *value);
void requestData_add_paramList(RequestData *requestData,char *field,char *value);
void requestData_add_header(RequestData *requestData,char *field,char *value);
WebUser *new_WebUser();
RequestData *new_RequestData();
ResponseData *new_ResponseData();
void http_proces(Client *client);
URLFunc *web_search_url(char *url,int methods);
void web_set_httpResponseBody(WebUser *web_user,char *body);
void web_trans_response_data(Client *client);
char * web_make_response_string(WebUser *web_user);
char * web_make_response_header_string(WebUser *web_user);
UrlParamList * web_request_get_paramList(RequestData *requestData,char *paramName);
WEBFUNC(testfunc);






/**
 * llhttpsetting.c
 */
int on_chunk_header(llhttp_t* llhttp);
void llhttpSetting_init();
int on_header_field(llhttp_t* llhttp, const char *at, size_t length);
int on_header_value(llhttp_t* llhttp, const char *at, size_t length);
int on_url(llhttp_t* llhttp, const char *at, size_t length);
int httpRequestReceivedDone();
int handle_on_Onbody(llhttp_t* llhttp, const char *at, size_t length);
int handle_on_headers_complete(llhttp_t* llhttp);
int httpRequest_prase(WebUser *user,char *recive_data,int length);







/**
 * web_httpresponse.c
 */
int web_http_response_200(WebUser *user);
int web_http_response_400(WebUser *user);
int web_http_response_404(WebUser *user);
int web_http_response_500(WebUser *user);
void web_http_response_header_complied(WebUser *user);





#endif